package cn.edu.zjut.mina.xcmahjong;

import java.util.*;


import cn.edu.zjut.mina.interf.ICardBean;
import com.sun.org.apache.xpath.internal.operations.Bool;
import sun.management.Agent;

public class TestClass {
	//牌堆
	public static XcmjCardPile cardPile = new XcmjCardPile();
	public static List<ArrayList<ICardBean>> handCards = new ArrayList<ArrayList<ICardBean>>();

	// 测试：取牌
	//四万 五万 六万 一筒 二筒 三筒 九筒 九筒 九筒 西风 西风 西风 北风 北风
	public static void main(String[] args) {
		cardPile.init();

		ArrayList<ICardBean> type1=new ArrayList<ICardBean>();
		ArrayList<ICardBean> type2=new ArrayList<ICardBean>();
		ArrayList<ICardBean> type3=new ArrayList<ICardBean>();
		ArrayList<ICardBean> type4=new ArrayList<ICardBean>();

		XcmjCardBean card1=new XcmjCardBean();
		card1.setTypeInt(0);
		card1.setNumber(4);
		card1.setIndex(0);
		type1.add(card1);

		XcmjCardBean card2=new XcmjCardBean();
		card2.setTypeInt(0);
		card2.setNumber(4);
		card2.setIndex(0);
		type1.add(card2);

		XcmjCardBean card3=new XcmjCardBean();
		card3.setTypeInt(0);
		card3.setNumber(4);
		card3.setIndex(0);
		type1.add(card3);

		XcmjCardBean card4=new XcmjCardBean();
		card4.setTypeInt(1);
		card4.setNumber(1);
		card4.setIndex(0);
		type2.add(card4);

		XcmjCardBean card5=new XcmjCardBean();
		card5.setTypeInt(1);
		card5.setNumber(3);
		card5.setIndex(0);
		type2.add(card5);

		XcmjCardBean card6=new XcmjCardBean();
		card6.setTypeInt(1);
		card6.setNumber(2);
		card6.setIndex(0);
		type2.add(card6);

		XcmjCardBean card7=new XcmjCardBean();
		card7.setTypeInt(1);
		card7.setNumber(9);
		card7.setIndex(0);
		type2.add(card7);

		XcmjCardBean card8=new XcmjCardBean();
		card8.setTypeInt(1);
		card8.setNumber(9);
		card8.setIndex(0);
		type2.add(card8);

		XcmjCardBean card9=new XcmjCardBean();
		card9.setTypeInt(1);
		card9.setNumber(9);
		card9.setIndex(1);
		type2.add(card9);

		XcmjCardBean card10=new XcmjCardBean();
		card10.setTypeInt(3);
		card10.setNumber(5);
		card10.setIndex(0);
		type4.add(card10);

		XcmjCardBean card11=new XcmjCardBean();
		card11.setTypeInt(3);
		card11.setNumber(5);
		card11.setIndex(0);
		type4.add(card11);

		XcmjCardBean card12=new XcmjCardBean();
		card12.setTypeInt(3);
		card12.setNumber(5);
		card12.setIndex(1);
		type4.add(card12);

		XcmjCardBean card13=new XcmjCardBean();
		card13.setTypeInt(3);
		card13.setNumber(7);
		card13.setIndex(0);
		type4.add(card13);


		handCards.add(type1);// c
		handCards.add(type2);// i
		handCards.add(type3);// o
		handCards.add(type4);// w

		XcmjCardBean card14=new XcmjCardBean();
		card14.setTypeInt(3);
		card14.setNumber(7);
		card14.setIndex(1);


		TestClass testClass=new TestClass();
		testClass.testHu(card14);

			/*for (int i = 0; i < 13; i++) {
				XcmjCardBean cardBean = cardPile.getCard();
				int type = cardBean.getTypeInt();
				switch (type) {
					case 1:
						handCards.get(0).add(cardBean);
						break;
					case 2:
						handCards.get(1).add(cardBean);
						break;
					case 3:
						handCards.get(2).add(cardBean);
						break;
					case 4:
						handCards.get(3).add(cardBean);
						break;
					default:
						break;
				}


		}*/






	}

	public void testHu(ICardBean cardBean){

		for (List<ICardBean> temp : handCards)
			sortList(temp);

		for (int i = 0; i < 4; i++) {
			for (int j = 0; j < handCards.get(i).size(); j++) {
				System.out.print(Integer.toString(((XcmjCardBean)handCards.get(i).get(j)).getTypeInt())+
						Integer.toString(((XcmjCardBean)handCards.get(i).get(j)).getNumber())+" ");
			}
		}
		System.out.print(Integer.toString(cardBean.getTypeInt())+Integer.toString(cardBean.getNumber()));

		//ICardBean tile=cardPile.getCard();
		List<ICardBean> altTiles=new ArrayList<ICardBean>();
		if (canHu(cardBean,handCards,altTiles))
			System.out.println("--------------胡");
		else
			System.out.println("--------------没胡");

		for (List<ICardBean> temp : handCards)
			temp.clear();
	}


	// 判断是否可以胡
	public Boolean canHu(ICardBean tile, List<ArrayList<ICardBean>> handTiles,List<ICardBean> altTiles){
		if (haveThreeColorTile(tile,handTiles,altTiles)==3)
			return false;
		else if (canHuHelp(tile,handTiles))
			return true;
		else
			return false;
	}

	/*判断手牌加牌堆是否有三种颜色*/
	// 返回牌的类型数量
	public Integer haveThreeColorTile(ICardBean tile, List<ArrayList<ICardBean>> handTiles,List<ICardBean> altTiles){
		Boolean hasWan = false;		    //是否有万
		Boolean hasTong = false;		//是否有筒
		Boolean hasTiao = false;		//是否有条

		//判断手牌
		for (ArrayList<ICardBean> tempList : handTiles){
			if (tempList.size()!=0){
				int type=tempList.get(0).getTypeInt();
				switch (type){
					case 1:
						hasWan=true;
						break;
					case 2:
						hasTong=true;
						break;
					case 3:
						hasTiao=true;
						break;
					default:
						break;
				}
			}
		}

		// 判断牌堆
		for (ICardBean temp : altTiles){
			int type=temp.getTypeInt();
			if (type==1)
				hasWan=true;
			else if (type==2)
				hasTong=true;
			else if (type==3)
				hasTiao=true;
		}

		if (tile.getTypeInt()==1)
			hasWan=true;
		else if (tile.getTypeInt()==2)
			hasTong=true;
		else if (tile.getTypeInt()==3)
			hasTiao=true;

		int colorNum=0;
		if (hasWan)
			colorNum++;
		if (hasTong)
			colorNum++;
		if (hasTiao)
			colorNum++;

		return colorNum;

	}

	// 判断是否可以胡的核心函数
	// 默认handTiles.get(0)--万；handTiles.get(1)--筒；handTiles.get(2)--条；handTiles.get(3)--字
	public Boolean canHuHelp(ICardBean tile, List<ArrayList<ICardBean>> handTiles){
		// 添加tile并排序
		switch (tile.getTypeInt()){
			case 0:
				handTiles.get(0).add(tile);
				sortList(handTiles.get(0));
				break;
			case 1:
				handTiles.get(1).add(tile);
				sortList(handTiles.get(1));
				break;
			case 2:
				handTiles.get(2).add(tile);
				sortList(handTiles.get(2));
				break;
			case 3:
				handTiles.get(3).add(tile);
				sortList(handTiles.get(3));
				break;
		}

		for (int i=0;i<4;i++){
			// i=0:对在万中
			if ((handTiles.get(i).size()-2)%3==0){
				// i=0:筒、（条）、字三张牌成组
				if (handTiles.get((i+1)%4).size()%3==0 && handTiles.get((i+2)%4).size()%3==0 && handTiles.get((i+3)%4).size()%3==0){
					for (ICardBean temp : handTiles.get(i)){
						if (getNumOFCardNumber(handTiles.get(i),temp.getNumber())==2){
							ICardBean bean1=temp;
							handTiles.get(i).remove(temp);
							ICardBean bean2 = null;
							for (ICardBean myTile : handTiles.get(i)){
								if (myTile.getNumber()==temp.getNumber()){
									bean2=myTile;
								}
							}
							handTiles.get(i).remove(bean2);
							// 剩余牌型号成三张组牌
							if (isThreeGroup(handTiles.get(i)) && isThreeGroup(handTiles.get((i+1)%4))
									&& isThreeGroup(handTiles.get((i+2)%4)) && isThreeGroup(handTiles.get((i+3)%4))){
								return true;
							}
							handTiles.get(i).add(bean1);
							handTiles.get(i).add(bean2);
							sortList(handTiles.get(i));
						}
					}
				}else
					return false;
			}
		}
		return false;
	}



	// 判断三张牌是否为一组牌
	public Boolean isThreeGroup(List<ICardBean> tiles){
		if (tiles.size()==0)
			return true;
		//
		List<ICardBean> newTiles=new ArrayList<ICardBean>();
		for (ICardBean temp : tiles){
			newTiles.add(temp);
		}
		// 对newTiles按照number,index从小到大排序
		sortList(newTiles);

		// 判断顺序连接的三张牌，字则跳过
		if (newTiles.get(0).getTypeInt()!=3){
			for (int i=0;i<newTiles.size();i++){
				ICardBean cardBean=newTiles.get(i);
				// 第一张牌
				if (i==0){
					//  如果第一张牌在牌堆中的种类少于3张，则该张牌必须和另外两张牌拼成一串
					if (getNumOFCardNumber(newTiles,cardBean.getNumber())<3){
						// 若存在紧连接着的两张牌，则去除
						removeNode(newTiles,cardBean,1,2);
					}
				}else {
					//
					if (getNumOFCardNumber(newTiles,cardBean.getNumber())<3){
						removeNode(newTiles,cardBean,1,2);
						removeNode(newTiles,cardBean,-1,1);
						removeNode(newTiles,cardBean,-1,-2);
					}

				}
				//

			}
		}

		// 删完之后，如果没有牌，则说明newTiles都成串
		if (newTiles.size()==0)
			return true;
		// 判断重复的三张牌
		// 删除newTiles相同的三张牌
		for (int i=0;i<newTiles.size();i++){
			ICardBean temp= newTiles.get(i);
			if (getNumOFCardNumber(newTiles,temp.getNumber())==3){
				for (Iterator it=newTiles.iterator();it.hasNext();){
					ICardBean point= (ICardBean) it.next();
					if (point.getNumber()==temp.getNumber())
						it.remove();
				}
			}
		}
		// 删完之后，如果没有牌，则说明newTiles都成串
		if (newTiles.size()==0)
			return true;
		else
			return false;

	}

	// 对newTiles按照number,index从小到大排序
	public void sortList(List<ICardBean> tiles){
		Comparator<ICardBean> comparator=new Comparator<ICardBean>() {
			public int compare(ICardBean o1, ICardBean o2) {
				if (o1.getNumber()!=o2.getNumber()){
					return o1.getNumber()-o2.getNumber();
				}else {
					return o1.getIndex()-o2.getIndex();
				}
			}
		};
		Collections.sort(tiles,comparator);
	}

	// 存在是否存在紧连接着的两张牌，有则去除牌堆tiles中牌cardBean的近邻的两张牌
	public void removeNode(List<ICardBean> tiles,ICardBean cardBean,int next,int last ){
		ICardBean one_it=ifHaveIndex(tiles,cardBean,next);
		ICardBean two_it=ifHaveIndex(tiles,cardBean,last);
		if ((one_it!=null) && (two_it!=null)  ){
			// 删除第三张
			tiles.remove(two_it);
			// 删除第二张
			tiles.remove(one_it);
			// 删除第一张
			tiles.remove(cardBean);
		}
	}


	// 在同一类型（typeInt）的牌堆中计算某一种（number）牌的数量
	public Integer getNumOFCardNumber(List<ICardBean> tiles,int cardNumber){
		int sum=0;
		for (ICardBean temp : tiles){
			if (temp.getNumber()==cardNumber)
				sum++;
		}
		return sum;
	}


	// 判断在同一类型（typeInt）的牌堆中是否有比tile大index的牌,无则返回null，有则返回其索引
	public ICardBean ifHaveIndex(List<ICardBean> tiles,ICardBean tile,int index){
		Iterator<ICardBean> it;
		for (it=tiles.iterator();it.hasNext();){
			ICardBean cardBean=it.next();
			if (cardBean.getNumber()==tile.getNumber()+index)
				return cardBean;
		}
		return null;
	}

	// 判断tiles中含有type类型的数量
	public Integer getNumOFType(List<ICardBean> tiles, int type){
		int sum=0;
		for (ICardBean temp : tiles){
			if (temp.getTypeInt()==type)
				sum++;
		}
		return sum;
	}







}
